package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.purchase.TcgOfflinePurchaseInStorage;
import com.ys.entity.purchase.TcgPurchaseByOrderSon;
import com.ys.entity.storage.TccOfflineAssemble;
import com.ys.entity.storage.TccOfflineAssembleItem;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 线上组装报错单
 */
@Controller
@RequestMapping("/web/storage/tccOfflineAssemble")
public class WebTccOfflineAssembleController {

	@Autowired
	private CommonApiService<TccOfflineAssemble> commonApiService;

    /*
     * 异常组装
     */
    @GetMapping("/toError")
    public String toError(Model model, HttpServletRequest request, String bid) {
        try {
            TccOfflineAssemble entity = new TccOfflineAssemble().setBid(bid).queryFirst();
			
			List<Map<String, Object>> sonlist = new TccOfflineAssembleItem()
					.setPbid(bid)
					.addLeftJoin("TCG_OfflinePurchaseInStorage B ON B.Id = A.offlinePurchaseStorageId")
            		.addField("B.SubCode subCode")
            		.addField("B.SubName subName")
            		.addField("B.SubBomNumber subBomNumber")
                    .addField("0 subLeftNumber")
                    .addLeftJoin("TCG_PurchaseByOrderSon C ON C.Id = B.PurchaseOrderSonId")
                    .addLeftJoin("TCG_PurchaseByPlanSon E ON  E.Id = C.PurchasePlanSonId ")//采购计划子件
                    .addField("E.PurchaseAmount AS purchaseAmount")//子件采购数量
                    .addField("E.JgSl AS jgSl")//子件加购数量
                    .addField("E.SubTotalAmount AS subTotalAmount")//子件采购总数
                    .setOrderby("B.subCode ASC")
                    .queryMap();


            Map<String, Object> extraParam =  new TcgOfflinePurchaseInStorage()
                    .setId(Long.valueOf(sonlist.get(0).get("offlinePurchaseStorageId").toString()))
                    .addLeftJoin("TCG_PurchasePlanGoods B ON B.id = A.purchaseOrderGoodsId")
                    .addField("B.SpAmount parentPurchaseNumber")
                    .queryFirstMap();
            extraParam.put("bid",entity.getBid());
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCC_OfflineAssemble, sonlist);
            commonApiService.toEdit(model, entity.getId(), entity, MenuId.TCC_OfflineAssemble, extraParam, subGridConfig);
            return "storage/tccOfflineAssemble/toError";
        }catch (Exception e) {
            return W.error(model, e.getMessage());
        }
    }


}






